<?php
class ControllerCommonMenu extends Controller {
	public function index() {
		$this->load->language('common/menu');

		// Menu
		$this->load->model('catalog/category');

		$this->load->model('catalog/product');

		$data['categories'] = array();

		$categories = $this->model_catalog_category->getCategories(0);

		$this->load->model('catalog/product');

		foreach ($categories as $category) {
		    $product_category[] = $category['category_id'];
			if ($category['top']) {
				// Level 2
				$children_data = array();
                $products = array();
				$children = $this->model_catalog_category->getCategories($category['category_id']);
				$top_count = 0;
				foreach ($children as $child) {
					$filter_data = array(
						'filter_category_id'  => $child['category_id'],
						'filter_sub_category' => true
					);
                    $product_category[] = $child['category_id'];
					if ($child['top']) {
					    $top_count++;
                    }

                    $child_category_id = $child['category_id'];
					$sub_children = $this->model_catalog_category->getCategories($child_category_id);
                    $sub_children_data = array();
                    if ($sub_children) {
                        foreach ($sub_children as $item) {
                            $product_category[] = $item['category_id'];
                            $sub_children_data[] = array(
                                'title' => $item['name'],
                                'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $item['category_id'])
                            );
                        }
                    }

					$children_data[] = array(
						'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
						'title' => $child['name'],
						'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
                        'is_top' => $child['top'],
                        'sub_children' => $sub_children_data
					);
				}

                if ($product_category) {
                    $products = $this->model_catalog_product->getRecommendProducts($product_category, 4);
                }

                $products_data = array();
                if ($products) {
                    foreach ($products as $product) {

                        if ($product['image'] && is_file(DIR_IMAGE . $product['image'])) {
                            $image = resizeImage($product['image'], 200, 200);
                        } else {
                            $image = resizeImage('no_image.png', 200, 200);
                        }

                        if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
                            $price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
                        } else {
                            $price = false;
                        }

                        if ((float)$product['special']) {
                            $special = $this->currency->format($this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
                        } else {
                            $special = false;
                        }

                        $products_data[] = array(
                            'thumb' => $image,
                            'name' => $product['name'],
                            'price' => $price,
                            'special' => $special,
                            'href' => uriLink('product/product', 'product_id=' . $product['product_id'])
                        );
                    }
                }

				// Level 1
				$data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('product/category', 'path=' . $category['category_id']),
                    'top_count'=> $top_count,
                    'products' => $products_data
				);

			}
		}

		$route = isset($this->request->get['route']) ? $this->request->get['route'] : '';
		if (!$route || $route =='common/home') {
		    $data['menu_class'] = 'menu-home';
        } else {
            $data['menu_class'] = 'menu-other';
        }

        $data['menus'] = array();
		if (!is_mobile()) {
		    $data['home'] = HTTPS_SERVER;
		    $data['menus'] = $this->model_catalog_product->getMenus();
        }

		return $this->load->view('common/menu', $data);
	}
}
